﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitesDAL
{
    public class PhonghocDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Phonghoc");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Phonghoc {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Phonghoc)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Phonghoc '{0}', N'{1}', '{2}', N'{3}' ";
            sql = string.Format(sql, o.Maphong, o.Tenphong, o.Succhua, o.Diadiem);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Phonghoc)obj;
            var sql = "set dateformat dmy; execute sp_Update_Phonghoc '{0}', N'{1}', '{2}', N'{3}' ";
            sql = string.Format(sql, o.Maphong, o.Tenphong, o.Succhua, o.Diadiem);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"select * from tb_Phonghoc where Tenphong like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.Phonghoc o)
        {
            string sql;
            if (o.Maphong != "")
                sql = @"Select * From tb_Phonghoc Where Maphong like '%"+o.Maphong+"%' and Tenphong like N'%"+o.Tenphong+"%' and Succhua like '%"+o.Succhua+"%' and Diadiem like N'%"+o.Diadiem+"%'  ";
            else
                sql = @"Select * From tb_Phonghoc Where Tenphong like N'%" + o.Tenphong + "%' and Succhua like '%" + o.Succhua + "%' and Diadiem like N'%" + o.Diadiem + "%'  ";
            return ExecuteQuery(sql);
        }

        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(Maphong) as Ma From tb_Phonghoc";
            return ExecuteQuery(sql);
        }

        #endregion
    }
}
